<%@ page language="java" pageEncoding="UTF-8"%>
<script type="text/javascript">
	var archiveTypeTree;
	$(function() {
		$('#admin_archiveType_panel').panel({
			title : '文档分类',
			fit : true,
			border : false,
			tools : [ {
				iconCls : 'icon-add',
				handler : function() {
					addArchiveType();
				}
			}, {}, {
				iconCls : 'icon-remove',
				handler : function() {
					removeArchiveType();
				}
			}, {} ]
		});

		function addArchiveType() {
			$('#admin_archiveTypeAdd_addArchiveTypeForm input').val('');
			var selectNode = archiveTypeTree.tree('getSelected');
			if (selectNode != null) {
				var flag = archiveTypeTree.tree('isLeaf', selectNode.target);
				if (flag) {
					$.ajax({
						url : '${pageContext.request.contextPath}/archiveAction!queryArchiveList.action',
						data : {
							archiveType : selectNode.id
						},
						dataType : 'json',
						success : function(r) {
							if (r.success) {
								openArchiveTypeAddDlg(selectNode);
							} else {
								$.messager.alert('提示', r.msg);
							}
						}
					});
				} else {
					openArchiveTypeAddDlg(selectNode);
				}
			} else {
				$.messager.alert('提示', '请选择文档分类！');
			}
		}

		function removeArchiveType() {
			var selectNode = archiveTypeTree.tree('getSelected');
			if (selectNode != null) {
				var selectNodeId = selectNode.id;
				var rootNode = archiveTypeTree.tree('getRoot');
				var rootNodeId = rootNode.id;
				if (selectNodeId != rootNodeId) {
					var flag = archiveTypeTree.tree('isLeaf', selectNode.target);
					if (flag) {
						$.ajax({
							url : '${pageContext.request.contextPath}/treeAction!removeTreeNode.action',
							data : {
								type : '01',
								id : selectNode.id
							},
							dataType : 'json',
							success : function(r) {
								freshArchiveType();
								if (r.success) {
									$.messager.show({
										title : '提示',
										msg : r.msg
									});
								} else {
									$.messager.alert('提示', r.msg);
								}
							}
						});
					} else {
						$.messager.alert('提示', '只允许从叶子节点开始删除！');
					}
				} else {
					$.messager.alert('提示', '根节点不允许删除！');
				}
			} else {
				$.messager.alert('提示', '请选择删除节点！');
			}
		}

		archiveTypeTree = $('#admin_archiveType_tree').tree({
			url : '${pageContext.request.contextPath}/treeAction!queryAllTreeNode.action?type=01',
			parentField : 'pid',
			lines : true
		});

		function freshArchiveType() {
			archiveTypeTree.tree({
				url : '${pageContext.request.contextPath}/treeAction!queryAllTreeNode.action?type=01',
				parentField : 'pid',
			});
		}
		
		function openArchiveTypeAddDlg(selectNode) {
			var dlg = $('<div style="padding:20px 20px;" />').dialog({
				width : 400,
				height : 200,
				href : '${pageContext.request.contextPath}/pages/admin/archiveTypeAdd.jsp',
				modal : true,
				title : '文档分类新增',
				buttons : [ {
					text : '保存',
					handler : function() {
						$('#admin_archiveTypeAdd_addArchiveTypeForm').form('submit', {
							url : '${pageContext.request.contextPath}/treeAction!addTreeNode.action',
							success : function(r) {
								var obj = jQuery.parseJSON(r);
								if (obj.success) {
									freshArchiveType();
									dlg.dialog('close');
									$.messager.show({
										title : '提示',
										msg : obj.msg
									});
								} else {
									$.messager.alert('提示', obj.msg);
								}
							}
						});
					}
				}, {
					text : '取消',
					handler : function() {
						dlg.dialog('close');
					}
				} ],
				onClose : function() {
					$(this).dialog('destroy');
				},
				onLoad : function() {
					$('#admin_archiveTypeAdd_addArchiveTypeForm').form('load', {
						ptext : selectNode.text,
						pid : selectNode.id
					});
				}
			});
		}

	});
</script>
<div id="admin_archiveType_panel" style="padding:5px 5px 0 5px">
	<ul id="admin_archiveType_tree"></ul>
</div>